package com.lej.dataservice.club.comm

import androidx.appcompat.app.AppCompatActivity
import androidx.core.widget.doOnTextChanged
import androidx.lifecycle.lifecycleScope
import com.lej.core.common.PCacheInfo
import com.lej.core.utils.extention.getDrawableCompat
import com.lej.core.utils.extention.toast
import com.lej.core.widget.drawable.CustomDrawable
import com.lej.core.widget.drawable.getStateDrawable
import com.lej.dataservice.club.R
import com.lej.dataservice.club.databinding.HomeDialogRealNameAuthBinding
import com.lej.dataservice.club.http.apiService
import com.lej.dataservice.club.http.checkData
import com.lej.dataservice.club.http.handleMsg
import com.lej.theme.toast.CustomTips.showTips
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.animator.ScaleAlphaAnimator
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.enums.PopupAnimation
import com.lxj.xpopup.impl.FullScreenPopupView
import com.qmuiteam.qmui.kotlin.onClick
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 *  author: cjq
 *  date  : 2022/7/4 0004
 *  desc  :
 */
class RealNameAuthDialog(private val act: AppCompatActivity, private val finishBack: () -> Unit) :
    FullScreenPopupView(act) {
    override fun getImplLayoutId() = R.layout.home_dialog_real_name_auth

    override fun initPopupContent() {
        super.initPopupContent()
        val binding =
            HomeDialogRealNameAuthBinding.bind(centerPopupContainer.findViewById(R.id.cl_dialog))
        val grayDrawable = CustomDrawable(cornerRadius = 100, solidColor = R.color.themeGrayColor)
        binding.tvNext.background =
            getStateDrawable(
                grayDrawable,
                null,
                null,
                context.getDrawableCompat(R.drawable.ic_auth_next)
            )
        binding.etName.background = CustomDrawable(cornerRadius = 100, solidColor = R.color.white)
        binding.etCard.background = CustomDrawable(cornerRadius = 100, solidColor = R.color.white)

        binding.etName.doOnTextChanged { _, _, _, _ ->
            binding.tvNext.isEnabled =
                binding.etName.text?.isNotEmpty() == true && binding.etCard.text?.isNotEmpty() == true
        }
        binding.etCard.doOnTextChanged { _, _, _, _ ->
            binding.tvNext.isEnabled =
                binding.etName.text?.isNotEmpty() == true && binding.etCard.text?.isNotEmpty() == true
        }
        binding.ivClose.onClick { dismiss() }
        binding.tvNext.onClick {
            val name = binding.etName.text.toString()
            val number = binding.etCard.text.toString()
            act.userAuth(name, number)
        }
    }

    private fun AppCompatActivity.userAuth(name: String?, number: String?) {
        lifecycleScope.launch {
            runCatching {
                apiService.realNameAuth(name, number).checkData()
                dismiss()
                PCacheInfo.isAuth = true
                delay(200)
                showTips(R.string.home_nft_detail_trans_auth_hint_cert_success)
                finishBack()
            }.onFailure { it.handleMsg.toast() }
        }
    }

    override fun getPopupAnimator() =
        ScaleAlphaAnimator(popupContentView, PopupAnimation.NoAnimation)

    companion object {
        fun show(act: AppCompatActivity, finishBack: () -> Unit = {}): BasePopupView =
            XPopup.Builder(act)
                .hasStatusBar(false)
                .dismissOnTouchOutside(true)
                .dismissOnBackPressed(true)
                .asCustom(RealNameAuthDialog(act, finishBack))
                .show()
    }
}